package com.kuang.demo.service.wx;

import com.kuang.demo.bean.wx.*;
import com.kuang.demo.mapper.wx.ShoppingCartMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ACG
 * @date 2020-11-04 17:10
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService{

    @Autowired
    ShoppingCartMapper shoppingCartMapper;
    @Override
    public int addControl(ShoppingCartFastaddBean shoppingFastaddBean) {
        Subject subject = SecurityUtils.getSubject();
        String principal1=(String) subject.getPrincipal();
        Integer userId = userId(principal1);

        ShoppingCartAddGoodsBean shoppingGoodsBean =  shoppingCartMapper.selectGoodsInformationByGoodsId(shoppingFastaddBean.getGoodsId());
       String name = shoppingCartMapper.selectGoodsNameByGoodsId(shoppingFastaddBean.getGoodsId());
       shoppingGoodsBean.setName(name);
        shoppingGoodsBean.setProductId(shoppingFastaddBean.getProductId());
        shoppingGoodsBean.setNumber(shoppingFastaddBean.getNumber());
        shoppingGoodsBean.setUserId(userId);
        shoppingCartMapper.InsertIntoShoppingCart(shoppingGoodsBean);//todo
      int sum = goodsCountControl();
        return sum;
    }

    @Override
    public ShoppingCartIndexData indexControl() {
        Subject subject = SecurityUtils.getSubject();
        String principal1=(String) subject.getPrincipal();
        Integer userId = userId(principal1);
        List<ShoppingCartIndexData.CartListBean> cartList =shoppingCartMapper.selectCartList(userId);


        ShoppingCartIndexData.CartTotalBean cartTotal1 =  shoppingCartMapper.selectCartTotal(userId);//checked =1

        ShoppingCartIndexData.CartTotalBean cartTotal2 =   shoppingCartMapper.selectCartTotalAndCheckIsZero(userId);//checked=0

        ShoppingCartIndexData.CartTotalBean cartTotalBean = new ShoppingCartIndexData.CartTotalBean();
        cartTotalBean.setCheckedGoodsAmount(cartTotal1.getCheckedGoodsAmount());
        cartTotalBean.setCheckedGoodsCount(cartTotal1.getCheckedGoodsCount());
        cartTotalBean.setGoodsAmount(cartTotal2.getGoodsAmount());
        cartTotalBean.setGoodsCount(cartTotal2.getGoodsCount());

        ShoppingCartIndexData shoppingCartIndexData = new ShoppingCartIndexData();
        shoppingCartIndexData.setCartList(cartList);
        shoppingCartIndexData.setCartTotal(cartTotalBean);
        return shoppingCartIndexData;
    }

    @Override
    public ShoppingCartIndexData checkedControl(ShoppingCartCheckedBean shoppingCartCheckedBean) {
       shoppingCartMapper.updateCheckedByProductId(shoppingCartCheckedBean);
        return indexControl();
    }

    @Override
    public ShoppingCartIndexData deleteControl(ShoppingCartCheckedBean shoppingCartCheckedBean) {
        shoppingCartMapper.updateDeleteByProductId(shoppingCartCheckedBean);
        return indexControl();
    }

    @Override
    public ShoppingCartCheckoutData checkoutControl(Integer cartId, Integer addressId, String couponId, String grouponRulesId) {
        Subject subject = SecurityUtils.getSubject();
        String principal1=(String) subject.getPrincipal();
        Integer userId = userId(principal1);
        ShoppingCartCheckoutData shoppingCheckoutData = new ShoppingCartCheckoutData();
        ShoppingCartCheckoutData.CheckedAddressBean checkedAddressBean =null;
        List<ShoppingCartCheckoutData.CheckedGoodsListBean> checkedGoodsList = null;
        //总价goodsTotalPrice
        int goodsTotalPrice;
        if (addressId==0){
            List<ShoppingCartCheckoutData.CheckedAddressBean> checkedAddressBeanList = shoppingCartMapper.selectCheckedAddressByUserId(userId);
            checkedAddressBean = checkedAddressBeanList.get(0);
        }else {
             checkedAddressBean =shoppingCartMapper.selectCheckedAddreeByAddressId(addressId);
        }
        if (cartId ==0){
            checkedGoodsList = shoppingCartMapper.selectCheckedGoodsListByUserId(userId);
            goodsTotalPrice = shoppingCartMapper.selectSum4GoodsCheckout(userId);
        }else {
            checkedGoodsList = shoppingCartMapper.selectGoodsListByCardId(cartId);
            goodsTotalPrice = shoppingCartMapper.selectGoodsSum4GoodsCheckout(cartId);
        }
        //可用优惠劵的数量 availableCouponLength
        int availableCouponLength = shoppingCartMapper.selectCountCouponCanUse(goodsTotalPrice);
        //   优惠卷 freightPrice double freightPrice = 0;
        int freightPrice ;
        if (goodsTotalPrice>=88){
            freightPrice = 0;
        }else {
            freightPrice=8;
        }
        Integer couponPrice =shoppingCartMapper.selectCouponPrice(goodsTotalPrice);
         List<Integer> couponId1 =shoppingCartMapper.selectCouponId(couponPrice);
        Integer couponId2 = couponId1.get(0);
        //折后价actualPrice: 80
        int actualPrice = goodsTotalPrice+freightPrice-couponPrice;


        shoppingCheckoutData.setCheckedGoodsList(checkedGoodsList);
        shoppingCheckoutData.setCheckedAddress(checkedAddressBean);
        shoppingCheckoutData.setAddressId(addressId);
        shoppingCheckoutData.setGrouponPrice(0);
        shoppingCheckoutData.setGrouponRulesId(0);
        shoppingCheckoutData.setGoodsTotalPrice(goodsTotalPrice);
        shoppingCheckoutData.setAvailableCouponLength(availableCouponLength);
        shoppingCheckoutData.setFreightPrice(freightPrice);
        shoppingCheckoutData.setCouponId(couponId2);
        shoppingCheckoutData.setCouponPrice(couponPrice);
        shoppingCheckoutData.setActualPrice(actualPrice);
        shoppingCheckoutData.setOrderTotalPrice(actualPrice);

        return shoppingCheckoutData;
    }

    @Override
    public Integer fastaddControl(ShoppingCartFastaddBean shoppingFastaddBean) {
        Subject subject = SecurityUtils.getSubject();
        String principal1=(String) subject.getPrincipal();
        Integer userId = userId(principal1);
        ShoppingCartAddGoodsBean shoppingGoodsBean =  shoppingCartMapper.selectGoodsInformationByGoodsId(shoppingFastaddBean.getGoodsId());
        shoppingGoodsBean.setProductId(shoppingFastaddBean.getProductId());
        shoppingGoodsBean.setNumber(shoppingFastaddBean.getNumber());
        String name = shoppingCartMapper.selectGoodsNameByGoodsId(shoppingFastaddBean.getGoodsId());
        shoppingGoodsBean.setName(name);
        shoppingGoodsBean.setUserId(userId);
        shoppingCartMapper.InsertIntoShoppingCart(shoppingGoodsBean);
//       Integer Id = shoppingCartMapper.selectShoppingCartId(shoppingFastaddBean.getGoodsId());
        return shoppingGoodsBean.getId();
    }

    @Override
    public void updateControl(ShoppingCartUpdateGoodsBean shoppingCartUpdateGoodsBean) {
        shoppingCartMapper.updateShoppingCartGoodsNumberById(shoppingCartUpdateGoodsBean.getId(),shoppingCartUpdateGoodsBean.getNumber());
    }

    @Override
    public Integer goodsCountControl() {
        Subject subject = SecurityUtils.getSubject();
        String principal1=(String) subject.getPrincipal();
        Integer userId = userId(principal1);
       Integer goodsNum =  shoppingCartMapper.selectGoodsSum(userId);
        return goodsNum;
    }

    public Integer userId(String principal1) {
      Integer userId = shoppingCartMapper.selectUserIdByUserName(principal1);
        return userId;
    }
}
